package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.ExcelHeaderMapper;
import com.csun.dsms.mapper.HgFixedAssetDataMapper;
import com.csun.dsms.model.huang.HgFixedAssetData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.model.huang.HgYearMonthVo;
import com.csun.dsms.service.HgFixedAssetDataService;
import com.csun.dsms.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgFixedAssetDataServiceImpl extends BaseService<HgFixedAssetData>
        implements HgFixedAssetDataService {

    @Resource
    private HgFixedAssetDataMapper hgFixedAssetDataMapper;
    @Resource
    private ExcelHeaderMapper excelHeaderMapper;

    @Override
    public List<HgFixedAssetData> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgFixedAssetDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public List<HgYearMonthVo> groupByMonth(HgQueryDto hgQueryDto) {
        return hgFixedAssetDataMapper.groupByMonth(hgQueryDto);
    }

    @Override
    public void exportFixedAssetData(HttpServletResponse response, HgQueryDto hgQueryDto) {
        try{
            ExcelWriter writer = ExcelUtil.getWriter();
            hgQueryDto.setPageSize(1000);
            List<HgFixedAssetData> hgFixedAssetData = hgFixedAssetDataMapper.queryListWithPage(hgQueryDto);
            if(CollectionUtil.isEmpty(hgFixedAssetData)){
                return;
            }
            excelHeaderMapper.queryTableComment("prod_dsms","hg_fai_data").forEach(map->{
                writer.addHeaderAlias(StringUtils.snakeToCamel(map.get("COLUMN_NAME")).toString(),map.get("COLUMN_COMMENT").toString());
            });
            writer.write(hgFixedAssetData, true);
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
            String fileName = "固定资产投资_" + sdf.format(new java.util.Date()) + ".xls";
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            ServletOutputStream out = response.getOutputStream();
            writer.flush(out, true);
            writer.close();
            IoUtil.close(out);
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    @Override
    public Wrapper importFaiData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgFixedAssetData hgFixedAssetData = new HgFixedAssetData();
                hgFixedAssetData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgFixedAssetData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgFixedAssetData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgFixedAssetData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgFixedAssetData.setInStockTotal(Convert.toDouble(excelMap.get("在库投资额")));
                hgFixedAssetData.setNewStockTotal(Convert.toDouble(excelMap.get("新入库投资额")));
                hgFixedAssetData.setGrowthRate(Convert.toDouble(excelMap.get("增速")));
                hgFixedAssetData.setTopRate(Convert.toDouble(excelMap.get("上一级增速")));
                hgFixedAssetData.setGrowthRateRanking(Convert.toInt(excelMap.get("增长率排名")));
                hgFixedAssetData.setContributionRate(Convert.toDouble(excelMap.get("贡献率")));
                hgFixedAssetData.setContributionRateRanking(Convert.toInt(excelMap.get("综合排名")));
                hgFixedAssetData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgFixedAssetData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgFixedAssetData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                hgFixedAssetData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgFixedAssetData.setCreatedTime(new Date());
                hgFixedAssetDataMapper.insert(hgFixedAssetData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入数据成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

}
